﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using TransFormer.Common;
using TransFormer.Model;

namespace TransFormer.Web
{
    public partial class ProductSearch : System.Web.UI.Page
    {
        public List<ProductType> ProductTypes;
        public List<TransFormer.Model.Product> Products;
        private TransFormer.BLL.ProductType _bllProductType = new BLL.ProductType();
        private TransFormer.BLL.Product _bllProduct = new BLL.Product();
        public string PageStr;
        private BLL.KeyWords _bllKey = new TransFormer.BLL.KeyWords();
        protected void Page_Load(object sender, EventArgs e)
        {
            var model2 = _bllKey.GetModel(3);
            if (model2 != null)
            {
                HtmlMeta keywords = new HtmlMeta();
                keywords.Name = "keywords";
                keywords.Content = model2.KeyContent;
                Page.Header.Controls.Add(keywords);
                HtmlMeta desc = new HtmlMeta();
                desc.Name = "description";
                desc.Content = model2.PageDesc;
                Page.Header.Controls.Add(desc);
                Title = model2.Title;

            }
            if (!IsPostBack)
            {
                const int pageSize = 3;
                int pageIndex = 1;
                int.TryParse(Request["pageIndex"], out pageIndex);
                int count = 0;
                ProductTypes = _bllProductType.GetModelList("Language='english'");
                var sInput = Request["searchInput"];
                if (sInput != null)
                {
                    Products = SearchProduct(sInput)
                        .Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                    count = _bllProduct.GetRecordCount("ProductName like '%" + sInput + "%' ");
                    PageStr = LaomaPager.ShowPageNavigate(pageSize, pageIndex, count,
                                                          "productSearch.aspx?searchInput=" + Request["searchInput"]);
                }
            }
        }

        private List<Model.Product> SearchProduct(string sInput)
        {
            BLL.Product bll = new BLL.Product();
            if (!string.IsNullOrEmpty(sInput))
                sInput = "%" + sInput + "%";
            //Products = bll.GetModelList("ProductName like '%" + sInput + "%' ");
            return bll.GetProductList(null, sInput);
        }
    }
}